﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Orion.Models
{
	public class QuestionRepository
	{

		private OrionDBDataContext m_db = new OrionDBDataContext();

		public IEnumerable<Question> GetQuestions()
		{
			var result = from q in m_db.Questions
						 orderby q.QuestionDate descending
						 select q;

			return result;
		}

		public Question GetQuestionById(int id)
		{
			var result = (from q in m_db.Questions
						  where q.ID == id
						  select q).SingleOrDefault();

			return result;
			
		}

		public void AddQuestions(Question q)
		{

			m_db.Questions.InsertOnSubmit(q);
			m_db.SubmitChanges();

		}

		public void AddQuestions(List<Question> manyQuestions)
		{
			foreach (var s in manyQuestions)
			{
				m_db.Questions.InsertOnSubmit(s);
			}
			m_db.SubmitChanges();

		}

		public void UpdateQuestion(Question ad)
		{
			Question oldQuestions = GetQuestionById(ad.ID);
			if (oldQuestions != null)
			{
				oldQuestions.Title = ad.Title;
				oldQuestions.Email = ad.Email;
				oldQuestions.Question1 = ad.Question1;
				oldQuestions.QuestionDate = ad.QuestionDate;
				oldQuestions.Recipient = ad.Recipient;
				oldQuestions.Status = ad.Status;
				oldQuestions.Telephone = ad.Telephone;
				oldQuestions.Answer = ad.Answer;

				m_db.SubmitChanges();

			}
		}

		internal void UpdateQuestion(EmailMessage id)
		{
			throw new NotImplementedException();
		}
	}
}